﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Xml;

namespace Sub.Function
{
     [DataContract]
    public class ActionService
    {   
       Sub.Function.EnumType.eActionErrorType _ErrorType = Sub.Function.EnumType.eActionErrorType.None;
        string _SysErrorMessage;
        bool _error = false;
        TokenResult _token;
        
        [DataMember]
        public bool Error
        {
            get
            {
                return _error;
            }
            set
            {
                _error = value;
            }

        }
        List<ActionResult> _results;
        [DataMember]
        public List<ActionResult> ErrorActions
        {
            get
            {
                if (_results == null)
                {
                    _results = new List<ActionResult>();
                }
                return _results;
            }
            set
            {
                _results = value;
            }
        }
        string _Entities = "";
        [DataMember]
        public string Entities
        {
            get
            {
                return _Entities;
            }
            set
            {
                _Entities = value;
            }
        }
        [DataMember]
        public string ErrorMessage
        {
            get
            {
                return _SysErrorMessage;
            }
            set
            {
                _SysErrorMessage = value;
            }
        }
        [DataMember]
        public TokenResult  Token
        {
            get
            {
                //if (_token!=null)
                //{
                //    _token.ServiceAction = this; 
                //}                
                return _token;
            }
            set
            {
                _token = value;
            }

        }
        public string ViewID
        {
            get
            {
                string XML = Sub.Function.Associate.Decrypt(_token.Data);
                XmlDocument doc = new XmlDocument();
                doc.LoadXml("<root>" + XML + "</root>");
                var node = doc.DocumentElement.SelectSingleNode("//View");
                if (node != null)
                {
                    return node.InnerText;
                }
                else
                {
                    return "";
                }
            }

        }
        public Guid UserID
        {
            get
            {
                string XML = Sub.Function.Associate.Decrypt(_token.Data);
                XmlDocument doc = new XmlDocument();
                doc.LoadXml("<root>" + XML + "</root>");
                var node = doc.DocumentElement.SelectSingleNode("//User");
                if (node != null)
                {
                    return new Guid(node.InnerText);
                }
                else
                {
                    return Guid.Empty;
                }
            }

        }
        [DataMember]
        public Sub.Function.EnumType.eActionErrorType ErrorType
        {
            get
            {
                return _ErrorType;
            }
            set
            {
                _ErrorType = value;
            }
        }
        [DataMember] 
        public string DataAssemblyName { get; set; }

         [DataMember] 
        public string DataEntityName { get; set; }
         [DataMember] 
         public string SQL { get; set; }
         [DataMember] 
         public Sub.Function.EnumType.eActionErrorType DataErrorType { get; set; }
         [DataMember] 
         public string DataErrorField { get; set; }
         [DataMember]
         public string UrlRequest { get; set; }
    }

}
